package com.jt.www.enums.enumSerializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.jt.www.enums.BaseEnum;

import java.io.IOException;
import java.util.Optional;

/**
 * 描述： <br>
 * 创建日期：2018-08-29 <br>
 * 修改日期：2018-08-29 下午8:13:20 <br>
 * 作者：wangyang <br>
 */
public class BaseEnumSerializer extends JsonSerializer<BaseEnum> {

    @Override
    public void serialize(BaseEnum value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException {
        Optional<BaseEnum> data = Optional.of(value);
        if (data.isPresent()) {
            jgen.writeObject(data.get().toMap());
        } else {
            jgen.writeString("");
        }
    }

}
